/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Order;
import com.hboxs.asl.dao.PromotionDao;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.PromotionService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Service - 促销
 */
@Service("promotionServiceImpl")
public class PromotionServiceImpl extends BaseServiceImpl<Promotion, Long> implements PromotionService {

    @Resource(name = "promotionDaoImpl")
    private PromotionDao promotionDao;

    @Resource(name = "promotionDaoImpl")
    public void setBaseDao(PromotionDao promotionDao) {
        super.setBaseDao(promotionDao);
    }

    @Transactional(readOnly = true)
    public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters, List<Order> orders) {
        return promotionDao.findList(hasBegun, hasEnded, count, filters, orders);
    }

    @Transactional(readOnly = true)
    @Cacheable("promotion")
    public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters, List<Order> orders, String cacheRegion) {
        return promotionDao.findList(hasBegun, hasEnded, count, filters, orders);
    }

    @Override
    public Promotion findUsingOne(Promotion.Type type) {
        return promotionDao.findUsingOne(type);
    }

    @Override
    public Promotion findUsingOne(Merchant merchant, Promotion.Type type) {
        return promotionDao.findUsingOne(merchant, type);
    }

    @Override
    public List<Promotion> findUsingList() {
        return promotionDao.findUsingList();
    }

    @Override
    public List<Promotion> findUsingList(Merchant merchant) {
        return promotionDao.findUsingList(merchant);
    }

    @Override
    public List<Promotion> findListNotInCart(Cart cart) {
        List<Promotion> ps = new ArrayList<>();
        for (Promotion p : findUsingList()) {
            if (!cart.getPromotions().contains(p)) {
                ps.add(p);
            }
        }
        return ps;
    }

    @Override
    public Set<Product> findProductByPromotion(Promotion promotion, Cart cart) {
        BigDecimal subtotalWithPromotion = cart.getSubtotalWithPromotion(promotion);
        BigDecimal minPrice = promotion.getMinimumPrice();
        Set<Goods> results = new HashSet<>();
        Set<Product> results_p = new HashSet<>();
        switch (promotion.getType()) {

            // 包邮
            case freeShipping:

                if (minPrice.compareTo(subtotalWithPromotion) >= 0) {
                    BigDecimal scopePrice = minPrice.subtract(subtotalWithPromotion);
                    Set<Product> ps = promotion.getAllProduct();

                    for (Product p : ps) {
                        if (p.getPrice().compareTo(scopePrice) >= 0) {
                            results.add(p.getGoods());
                        }
                    }
                }

                break;


            // 满减
            case subtract:
                if (minPrice.compareTo(subtotalWithPromotion) >= 0) {
                    BigDecimal scopePrice = minPrice.subtract(subtotalWithPromotion);
                    Set<Product> ps = promotion.getAllProduct();
                    for (Product p : ps) {
                        if (p.getPrice().compareTo(scopePrice) >= 0) {
                            results.add(p.getGoods());
                        }
                    }
                }

                break;

        }

        for (Goods g : results) {
            if (g.getProducts() != null) {
                results_p.add(g.getProducts().get(0));
            }

        }

        return results_p;
    }


    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public void save(Promotion promotion) {
        super.save(promotion);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public Promotion update(Promotion promotion) {
        return super.update(promotion);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public Promotion update(Promotion promotion, String... ignoreProperties) {
        return super.update(promotion, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public void delete(Long... ids) {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"promotion", "product"}, allEntries = true)
    public void delete(Promotion promotion) {
        super.delete(promotion);
    }

}